// utils/Component/subject/ChoiceQuestion/ChoiceQuestion.js
Component({

  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    rubric: "",
    show: false,
    title: ["年级", "班级", "课程", "班级"],
    dataArr: [],
    filterInfo: [],
    zhi: [],
    chuanzhi: '',
    ur: '',
    allItems: [],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    close: function () {
      this.setData({
        backdrop: false,
        show: false
      })
      //this.triggerEvent("confirm");
    },
    isShow: function (e) {
      this.setData({
        backdrop: !this.data.show,
        show: !this.data.show
      })
    },
    // 点击条件列表
    isSelectbanji: function (e) {
      let id = e.currentTarget.dataset.id;
      console.log(id)
      var index = e.currentTarget.dataset.index
      var name = e.currentTarget.dataset.name
      this.data.allItems[index] = this.data.dataArr[index][0][0][0] + "|" + this.data.dataArr[index][0][0][1] + ":" + name + "|" + id
      if(name=="全部"){
        this.data.nameitems[index] = ""
      }else{
        this.data.nameitems[index] = name
      }
     
      var chzhi = this.data.allItems.toString();
      var chzhi2 = this.data.nameitems.toString();
     // console.log(chzhi2)
     var newchzhi=[];
     newchzhi[0] = chzhi;
     newchzhi[1] = chzhi2
      this.setData({
        ur: newchzhi
      })
     // console.log(this.data.ur)
      this.triggerEvent("confirmEvent", this.data.ur);
    },

    //渲染页面函数
    conditionList: function (filterInfo1, ur) {
      // 数据返回的值 filterInfo1 res
      // 条件搜索 ur
      // 例如：课本类型|课本类型id:沪教版|df885e2c-c784-4316-8926-3ec147d4740c,年级|年级id:全部|,出版社|出版社id:全部|,科目id|科目idid:全部|,学期|学期id:全部|
      this.setData({
        filterInfo: JSON.parse(filterInfo1[0].childrenJson),
        ur: ur,
      })
      var dataArr = [];
      let urlArr = ur.split(/[,:]/);
      for (let k = 0; k < this.data.filterInfo.length; k++) {
        var j = 0;
        for (var key in this.data.filterInfo[k]) {
          if (k == 0) {
            this.data.dataArr[j] = [];
            this.data.dataArr[j][0] = [];               			//每个条件项建立两个数组，数组0用于存放当前选中项和父级项
            this.data.dataArr[j][1] = [];   						//数组1存放兄弟项或者子级项
            this.data.dataArr[j][0].push(key);
            this.data.dataArr[j][0].push("全部|");                //初始的条件名称和全部按钮
          }
          let childArr = this.data.filterInfo[k][key].split("$");
          for (let k = 0; k < this.data.filterInfo.length; k++) {
            let j = 0;
            let n = 1;
            for (let key in this.data.filterInfo[k]) {
              if (k === 0) {
                this.data.dataArr[j] = [];
                this.data.dataArr[j][0] = [];               			//每个条件项建立两个数组，数组0用于存放当前选中项和父级项
                this.data.dataArr[j][1] = [];   						//数组1存放兄弟项或者子级项
                this.data.dataArr[j][0].push(key);
                this.data.dataArr[j][0].push("全部|");                //初始的条件名称和全部按钮
              }
              let childArr = this.data.filterInfo[k][key].split("$");
              for (let a = 0; a < childArr.length; a++) {
                let getsign = true;
                if (childArr.length > 1 && a < (childArr.length - 1)) {
                  for (let i = 0; i < this.data.dataArr[j][0].length; i++) {
                    if (this.data.dataArr[j][0][i] === childArr[a]) {
                      getsign = false;
                    }
                  }
                  if (getsign === true) {
                    this.data.dataArr[j][0].push(childArr[a]);
                  }
                } else {
                  for (let i = 0; i < this.data.dataArr[j][0].length; i++) {
                    if (this.data.dataArr[j][0][i] === childArr[a]) {
                      getsign = false;
                    }
                  }
                  for (let i = 0; i < this.data.dataArr[j][1].length; i++) {
                    if (this.data.dataArr[j][1][i] === childArr[a]) {
                      getsign = false;
                    }
                  }
                  if (getsign === true) {
                    if (childArr[a] === urlArr[n]) {
                      this.data.dataArr[j][0].push(childArr[a]);
                    } else {
                      this.data.dataArr[j][1].push(childArr[a]);
                    }
                  }
                }
              }
              for (let x = 0; x < this.data.dataArr[j][0].length; x++) {
                for (let y = 0; y < this.data.dataArr[j][1].length; y++) {
                  if (this.data.dataArr[j][0][x] == this.data.dataArr[j][1][y]) {
                    this.data.dataArr[j][1].splice(y, 1); //利用splice函数删除元素，从第i个位置，截取长度为1的元素
                  }
                }
              }
              j += 1;
              n += 2;
            }
          }
        }
      }
      for (var i = 0; i < this.data.dataArr.length; i++) {
        for (var j = 0; j < this.data.dataArr[i].length; j++) {
          for (var k = 0; k < this.data.dataArr[i][j].length; k++) {
            this.data.dataArr[i][j][k] = this.data.dataArr[i][j][k].split('|');
          }
        }
      }
      this.setData({ dataArr: this.data.dataArr });
      console.log(this.data.dataArr)      

      if(ur==""||ur==undefined){
        var allitems = [];
        var nameitems=[];
        for (let p = 0; p < this.data.dataArr.length; p++) {
          allitems[p] = this.data.dataArr[p][0][0][0] + '|' + this.data.dataArr[p][0][0][1] + ':' + this.data.dataArr[p][0][1][0] + '|' + this.data.dataArr[p][0][1][1]
          nameitems[p]=''
        }
        this.setData({
          allItems: allitems,
          nameitems: nameitems
        })
       // console.log("nameitems", this.data.nameitems)
      }else{    
        console.log(ur)
      }
    },

  }
})
